package com.atguigu.sparsearray;

import java.io.*;

/**
 * @author 程旭东
 */
public class SparseArray {
    public static void main(String[] args) throws IOException {
        int[][] chessArr = new int[11][11];
        chessArr[0][3] = 22;
        chessArr[0][6] = 15;
        chessArr[1][1] = 11;


        System.out.println("原始的二维数组");
        for(int[] rows : chessArr){
            for(int data:rows){
                //先将需要进行散列的数组遍历出来
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }

        int sum = 0;
        for(int[] rows : chessArr){
            for(int data:rows){
                if(data!=0){
                    sum++;
                }
            }
        }

        System.out.println(sum);

        int[][] sparseArr = new int[sum+1][3];
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;

        //用于记录sparseArr的当前行数
        int count = 0;

        for(int i=0;i<11;i++){
            for(int j=0;j<11;j++){
                if(chessArr[i][j]!=0){
                    count++;
                    sparseArr[count][0]=i;
                    sparseArr[count][1]=j;
                    sparseArr[count][2]= chessArr[i][j];
                }
            }
        }

        //将稀疏数组存入磁盘中的map.data
        OutputStreamWriter osw =null;
        File file = new File("src/com/atguigu/sparsearray/map.data");
        try {

            FileOutputStream fos = new FileOutputStream(file);
            osw =  new OutputStreamWriter(fos);
            for (int i = 0; i < sparseArr.length; i++) {
                if (i== sparseArr.length-1){
                    osw.write(sparseArr[i][0]+","+sparseArr[i][1]+","+sparseArr[i][2]);
                }else {
                    osw.write(sparseArr[i][0]+","+sparseArr[i][1]+","+sparseArr[i][2]+",");
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(osw!=null){
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        //将存入磁盘中的map.data文件中稀疏数组数据取出来
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader ir = new InputStreamReader(fis);
        StringBuffer sb = new StringBuffer();
        while (ir.ready()){
            sb.append((char) ir.read());//加入stringbuffer，转为char,不然只会打印地址
        }
        System.out.println("读出的文件"+sb.toString());
        ir.close();
        fis.close();

        //把读出的文件，赋值给稀疏数组
        String[] str = sb.toString().split(",");
        System.out.println(str.length/3);
        int sparseArray3[][]= new int[str.length/3][3];
        int m= 0;
        for (String  s:str) {
            System.out.println(m%3);
            sparseArray3[m/3][m%3] =Integer.parseInt(s);
            m++;
        }


        System.out.println("得到的稀疏数组为：");
        for(int i=0;i<sparseArr.length;i++){
            System.out.printf("%d\t%d\t%d\t\n",sparseArray3[i][0],sparseArray3[i][1],sparseArray3[i][2]);
        }

        int value = sparseArr[0][2];
        int row = 1;

        //将稀疏数组恢复为原始的二维数组
        int[][] chessArr2 = new int[sparseArr[0][0]][sparseArr[0][1]];
        for(int i=1;i<sparseArr.length;i++){
            chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }

        System.out.println("恢复后的稀疏数组为：");
        for(int[] rows : chessArr2){
            for(int data:rows){
                //先将需要进行散列的数组遍历出来
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }


    }
}
